home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / dev / fsystem-1.2 / frealio.mod < prev    next >
Encoding:
Text File  |  1994-05-22  |  5.4 KB  |  188 lines

  1. (* (* $VER: fRealIO 1.1 (21-May-94) Copyright © by Lars Düning *) *)
  2.  
  3. MODULE fRealIO;
  4.  
  5. (*---------------------------------------------------------------------------
  6. ** File-IO of REAL numbers for Amiga-Oberon.
  7. **
  8. ** Copyright © 1991-1994  Lars Düning  -  All rights reserved.
  9. ** Permission granted for non-commercial use.
  10. **---------------------------------------------------------------------------
  11. ** CREDIT:
  12. **   This module evolved from RealIO of Amiga-Oberon v1.17.1
  13. **---------------------------------------------------------------------------
  14. ** Oberon-2: Amiga-Oberon v3.10, F. Siebert / A+L AG
  15. **---------------------------------------------------------------------------
  16. ** [lars] Lars Düning; Am Wendenwehr 25; D-38114-Braunschweig;
  17. **                     Germany; Tel. 49-531-345692
  18. **---------------------------------------------------------------------------
  19. ** 01-Jan-91 [lars]
  20. ** 21-May-04 [lars] Moved ParseReal() herein.
  21. **---------------------------------------------------------------------------
  22. *)
  23.  
  24. IMPORT
  25.   (* $IF Debug *) Debug, (* $END *)
  26.   fio, fs:FSystem, rc: RealConversions;
  27.  
  28. (*-------------------------------------------------------------------------*)
  29. PROCEDURE ParseReal * ( VAR in : fio.File; VAR str : ARRAY OF CHAR) : BOOLEAN;
  30.  
  31. (* Parse a REAL number from a file.
  32. **
  33. ** Arguments:
  34. **   in  : the file to read from.
  35. **   str : buffer taking the number string.
  36. **
  37. ** Result:
  38. **   TRUE on success, else FALSE with in.status denoting the error.
  39. **     If FALSE is returned, but in.status is 'ok', then no correct number
  40. **     could be read.
  41. **
  42. ** Leading whitespace are ignored, trailing linebreaks are not read.
  43. *)
  44.  
  45. VAR
  46.   i      : INTEGER;
  47.   ch     : CHAR;
  48.   isReal : BOOLEAN;
  49.  
  50. BEGIN
  51.   isReal := FALSE;
  52.   IF LEN(str) = 0 THEN RETURN FALSE; END;
  53.   i := 0;
  54.   WHILE i < LEN(str) DO str[i] := 0X; END;
  55.   i := 0;
  56.  
  57.   (* Skip any preceeding spaces/tabs *)
  58.   REPEAT
  59.     IF ~fio.Read(in, ch) THEN RETURN FALSE; END;
  60.   UNTIL (ch # ' ') AND (ch # '\t');
  61.  
  62.   (* Mantissen-Vorzeichen ? *)
  63.   IF (ch = "+") OR (ch = "-") THEN
  64.     str[i] := ch; INC (i); IF i = LEN (str) THEN RETURN FALSE; END;
  65.     IF ~fio.Read(in, ch) THEN RETURN FALSE; END;
  66.   END;
  67.  
  68.   (* Mantisse - integer part *)
  69.   LOOP
  70.     CASE ch OF
  71.     '0'..'9':
  72.       isReal := TRUE;
  73.       str[i] := ch; INC(i); IF i = LEN(str) THEN RETURN isReal; END;
  74.       IF ~fio.Read(in, ch) THEN RETURN FALSE; END;
  75.     ELSE EXIT END;
  76.   END;
  77.  
  78.   (* Decimal point with mantisse fraction? *)
  79.   IF ch = "." THEN
  80.     isReal := FALSE;
  81.     str[i] := ch; INC(i); IF i = LEN(str) THEN RETURN FALSE; END;
  82.     IF ~fio.Read(in, ch) THEN RETURN FALSE; END;
  83.  
  84.     LOOP
  85.       CASE ch OF
  86.       '0'..'9':
  87.         isReal := TRUE;
  88.         str[i] := ch; INC(i); IF i = LEN(str) THEN RETURN isReal; END;
  89.         IF ~fio.Read(in, ch) THEN RETURN FALSE; END;
  90.       ELSE EXIT END;
  91.     END;
  92.   END;
  93.  
  94.   (* Exponent? (assuming a legal float preceeded) *)
  95.   IF isReal AND ((ch = "E") OR (ch = "e")) THEN
  96.     isReal := FALSE;
  97.     str[i] := ch; INC(i); IF i = LEN(str) THEN RETURN FALSE; END;
  98.     IF ~fio.Read(in, ch) THEN RETURN FALSE; END;
  99.  
  100.     (* Sign of exponent? *)
  101.     IF (ch = "+") OR (ch = "-") THEN
  102.       str[i] := ch; INC (i); IF i = LEN (str) THEN RETURN FALSE; END;
  103.       IF ~fio.Read(in, ch) THEN RETURN FALSE; END;
  104.     END;
  105.  
  106.     LOOP
  107.       CASE ch OF
  108.       '0'..'9':
  109.         isReal := TRUE;
  110.         str[i] := ch; INC(i); IF i = LEN(str) THEN RETURN isReal; END;
  111.         IF ~fio.Read(in, ch) THEN RETURN FALSE; END;
  112.       ELSE EXIT END;
  113.     END;
  114.   END;
  115.  
  116.   RETURN fs.Backward(in, 1) & isReal;
  117. END ParseReal;
  118.  
  119. (*-------------------------------------------------------------------------*)
  120. PROCEDURE writeReal * {"fRealIO.WriteReal"}
  121.                       ( VAR    f : fio.File
  122.                       ;        r : REAL
  123.                       ;     v, n : INTEGER
  124.                       ;      exp : BOOLEAN
  125.                       );
  126. PROCEDURE WriteReal * ( VAR    f : fio.File
  127.                       ;        r : REAL
  128.                       ;     v, n : INTEGER
  129.                       ;      exp : BOOLEAN
  130.                       ): BOOLEAN;
  131.  
  132. (* Write a REAL number into a file.
  133. **
  134. ** Arguments:
  135. **   f  : the file to write to.
  136. **   r  : the number to write.
  137. **   v  : number of digits in front of the '.'
  138. **   n  : number of digits after the '.'
  139. **   exp: if TRUE, the 'E' notation is used when appropriate.
  140. **
  141. ** Result:
  142. **   TRUE on success, else FALSE with out.status denoting the error.
  143. **     If FALSE is returned, but out.status is 'ok', then the number is
  144. **     larger than the allowed number of digits.
  145. *)
  146.  
  147. VAR
  148.   str: ARRAY 256 OF CHAR;
  149.  
  150. BEGIN
  151.   IF rc.RealToString(r,str,v,n,exp) THEN
  152.     RETURN fio.WriteString(f,str);
  153.   ELSE
  154.     RETURN FALSE;
  155.   END;
  156. END WriteReal;
  157.  
  158. (*-------------------------------------------------------------------------*)
  159. PROCEDURE readReal * {"fRealIO.ReadReal"}(VAR f : fio.File; VAR r: REAL);
  160. PROCEDURE ReadReal * (VAR f : fio.File; VAR r: REAL): BOOLEAN;
  161.  
  162. (* Read a REAL number from a file.
  163. **
  164. ** Arguments:
  165. **   f  : the file to read from.
  166. **   r  : variable to take the number read.
  167. **
  168. ** Result:
  169. **   TRUE on success, else FALSE with out.status denoting the error.
  170. **     If FALSE is returned, but out.status is 'ok', then no correct number
  171. **     could be read.
  172. **   r: the number read.
  173. *)
  174.  
  175. VAR
  176.   str: ARRAY 256 OF CHAR;
  177.  
  178. BEGIN
  179.   IF ParseReal(f, str) THEN
  180.     RETURN rc.StringToReal(str,r);
  181.   END;
  182.   RETURN FALSE;
  183. END ReadReal;
  184.  
  185. END fRealIO.
  186.  
  187. (***************************************************************************)
  188.